
"""
    使用装饰器实现上下文管理器功能（yield关键字）
"""
from contextlib import contextmanager


@contextmanager
def my_file(file_name, file_method):

    print("enter")
    file1 = open(file_name, file_method)

    yield file1

    print("exit")
    file1.close()


if __name__ == '__main__':
    with my_file("test.txt", "r") as f:
        print(f.read())
